﻿var roomShopList = [];//购物车
var ShopListReady = false;//购物车加载情况

//初始化购物车
function SetRoomShopCart(data) {
    CleanShopCart();
    var inter = setInterval(function () {
            //房间数据加载完成之后，再显示购物车数据
            if (window.roomloadend) {
                for (var i in data) {
                    if (data.hasOwnProperty(i)) {
                        var cart = data[i];
                        roomShopList.push(
                            {
                                shopid: parseInt(cart.D_ID), //购物车编号
                                id: parseInt(cart.D_FieldID),
                                oper: cart.D_Operation,
                                state: cart.D_State,
                                time: cart.D_Time,
                                serial: cart.D_SerialNumber//流水号
                    });
                    }
                }
                ShopListReady = true;
                showRoomSeat();//显示房间购物车
                window.RoomSettlement();//房间结算
                clearInterval(inter);
            }
        },
        200);
}

//更新购物车状态
function SetRoomCartState(id, state) {
    var index = -1;
    for (var i in roomShopList) {
        if (roomShopList.hasOwnProperty(i)) {
            var room = roomShopList[i];
            if (room.id === id) {
                index = parseInt(i);
            }
        }
    }
    if (index !== -1) {
        if (roomShopList[index].state === 1) {
            RoomCartExpired(index);
        }
        roomShopList[index].state = state;
        showRoomSeat();
    }
}

//更新购物车状态
function SetRoomCartserial(id, state, serial) {
    var index = -1;
    for (var i in roomShopList) {
        if (roomShopList.hasOwnProperty(i)) {
            var room = roomShopList[i];
            if (room.id === id) {
                index = parseInt(i);
            }
        }
    }
    if (index !== -1 && state === 3) {
        roomShopList[index].serial = serial;
    }
}

//var shopstatearr = ["", "确认预定", "取消预定", "提交成功", "已过期", "销退"];
var shopstatearr = ["", "预定", "放弃", "确定", "过期", "销退"];
//显示房间购物车
function showRoomSeat() {
    for (var i in roomShopList) {
        if (roomShopList.hasOwnProperty(i)) {
            var id = roomShopList[i].id;
            var shopstate = roomShopList[i].state;
            var da = roomData[id];
            var lenth = $(".roomcart .cartcard").length;
            if (parseInt(i) >= lenth) {
                //新增购物车卡片
                var card = CreatCard(i, shopstatearr[shopstate]);
                card.attr("tid", id);
                $(".roomcart").append(card);
                var div = $("<div  id=\"down" +
                    i +
                    "\" class=\"timedown\"><div id=\"cartCountdown" +
                    i +
                    "\" class=\"time\"></div></div>");
                $("#cartcard" + i).append(div);
                $(".timedown").hide();
                $("#down" + i).show();

                var time = roomShopList[i].time;
                if (parseInt(shopstate) === 1) {
                    //倒计时
                    window.getCountDown({
                        obj: $('#cartCountdown' + i),
                        timestamp: time,
                        clear: true,
                        format: "%M:%S"
                    });
                }
                showRoomInGrid(da, parseInt(i));
            }
            //            $("#carttitle" + i + " .spanstate").html("(" + shopstatearr[shopstate] + ")");
            if (shopstate === 1) {
                BtnBindEvent(id);
                $("#cartcard" + i).addClass("cartcardin");
                $(".cart-button").appendTo("#cartcard" + i).show();
            } else if (shopstate === 3) {
                //确定房源
                $("#cartcard" + i).removeClass("cartcardin").addClass("cartcardsuc");
                BtnHide();
            } else {
                BtnHide();
            }
        }
    }
    //计算抢购次数
    var leavecount = window.cartmaxcount - roomShopList.length;
    ShowTip(leavecount);
}

//显示购物车表格
function showRoomInGrid(data, index) {
    if (data === undefined || data === null) return;
    var monitor = data.D_Monitor;
//    if (monitor === 0) {
        var title = data.D_BanName + " " + data.D_Unit + " " + data.D_RoomNo;
        $("#carttitle" + index).html(title);
        for (var i in cartrooninfo) {
            if (cartrooninfo.hasOwnProperty(i)) {
                var col = cartrooninfo[i];
                var id = col.Class + index;
                var field = col.Field;
                var html = "";
                eval("html = data." + field);
                $("#" + id).html(html);
            }
        }
//    }
}

//购物车房间信息列对象
var cartrooninfo = [
    { Key: "面积", Class: "dfloorarea", Field: "D_FloorArea" },
    { Key: "户型", Class: "dhousemodel", Field: "D_HouseModel" },
    { Key: "单价", Class: "dprice", Field: "D_Price" },
    { Key: "总价", Class: "dtotle", Field: "D_Total" }
];
//购物车卡片
function CreatCard(index) {
    var divcard = $("<div id=\"cartcard" + index + "\" class=\"cartcard\"></div>");
    var divtitle = $("<div id=\"carttitle" + index + "\" class=\"cart-title\"></div>");
    var divcontent = $("<div id=\"cartcontent" + index + "\" class=\"cart-content\"></div>");
    var divinfo = $("<div class=\"cart-info clearfix\"></div>");
    var ul = $("<ul></ul>");
    for (var i in cartrooninfo) {
        if (cartrooninfo.hasOwnProperty(i)) {
            var col = cartrooninfo[i];
            var li = "<li>" + col.Key + "</li>";
            li += "<li id=\"" + col.Class + index + "\"></li>";
            ul.append(li);
        }
    }

    divcard.append(divtitle);
    divcard.append(divcontent);
    divinfo.append(ul);
    divcontent.append(divinfo);
    return divcard;
}

//购物车按钮事件绑定
function BtnBindEvent(id) {
    //事件绑定
    //提交预定
    $("#submitOrder")
        .bind("click",
            function () {
                $("#submitOrder").unbind("click");
                UpdateShopCart(id, 1, $("#name").val(), window.user, $("#cardno").val(), $("#tel").val());
            });
    //取消预定
    $("#calcleOrder")
        .bind("click",
            function () {
                $("#submitOrder").unbind("click");
                UpdateShopCart(id, 2, $("#name").val(), window.user, $("#cardno").val(), $("#tel").val());
            });
}

function BtnHide() {
    $(".cart-button").hide();
    //取消事件绑定
    $("#submitOrder").unbind("click");
    $("#calcleOrder").unbind("click");
}

//购物车清空
function CleanShopCart() {
    roomShopList = [];
    $(".cartcard").remove();
    CleanCountDown();
}

//清空倒计时
function CleanCountDown() {
    $(".time").remove();
}

//停止倒计时
function CleanCountStop() {
    $(".time").remove();
}

//获取已预定房间数量
function GetOrderRoomCount() {
    var count = $(".cartcardsuc").length;
    return count;
}

//获取购物车下标
function getIndexByRoomCart(id) {
    var index = -1;
    for (var i in roomShopList) {
        if (roomShopList.hasOwnProperty(i)) {
            var cart = roomShopList[i];
            if (cart.id.toString() === id.toString()) {
                index = i;
            }

        }
    }
    return index;
}

//判断是否有购物车预定中
function GetRoomCartStateInOrder() {
    var count = 0;
    for (var i in roomShopList) {
        if (roomShopList.hasOwnProperty(i)) {
            var cart = roomShopList[i];
            if (cart.state === 1) {
                count ++;
            }
        }
    }
    return count;
}

//获取购物车编号
function GetRoomCartId(roomid) {
    var id = "";
    for (var i in roomShopList) {
        if (roomShopList.hasOwnProperty(i)) {
            var cart = roomShopList[i];
            if (cart.id.toString() === roomid.toString() && cart.state === 1) {
                console.log("cart");
                console.log(cart);
                id = cart.shopid;
            }

        }
    }
    return id.toString();
}

function RoomCartExpired(index) {
    $("#cartcard" + index).removeClass("cartcardin");
}

function roomcartAnimate() {
    var cart = $(".roomcart");
    var obj = $(".cartcard:last");
    $(document).innerHeight();
    var top = obj.offset().top;
    var left = obj.offset().left;
    var copy = cart.clone();
    copy.find(".cartcard:not(:last)").remove();
    copy.find(".timedown").remove();
    copy.find(".cart-button").remove();
    obj.hide();
    copy.css({
        position: "fixed",
        width: obj.width(),
        top: ($(document).innerHeight() - copy.height()) / 2,
        left: ($(document).innerWidth() - copy.width()) / 2,
        opacity: 0.1
    });
    $("body").append(copy);
    copy.animate({
        opacity: 1
    }, 150, function () {
        copy.animate({
            left: left + 20,
            top: top + 20,
            opacity: 0.3
        }, 600, function () {
            obj.show();
            copy.remove();
        });
    });
}